package class1;

import common.ArrayUtils;

import java.util.Arrays;

public class TestDemo1 {

    public static void main(String[] args) {
        int[] arr = {3, 5, 6, 3, 4, 5, 2, 6, 9, 0, 1};
        mergeSort(arr);
        ArrayUtils.printArr(arr);
    }


    public static void mergeSort(int[] arr) {
        process(arr, 0, arr.length - 1);
    }

    public static void process(int[] arr, int L, int R) {
        if (L == R) {
            return;
        }
        int mid = L + ((R - L) >> 1);
        process(arr, L, mid);
        process(arr, mid + 1, R);
        merge(arr, L, mid, R);
    }

    public static void merge(int[] arr, int L, int mid, int R) {
        int[] t = new int[R - L + 1];
        int p1 = L, p2 = mid + 1, i = 0;
        while (p1 <= mid && p2 <= R) {
            t[i++] = arr[p1] > arr[p2] ? arr[p2++] : arr[p1++];
        }
        while (p1 <= mid) {
            t[i++] = arr[p1++];
        }
        while (p2 <= R) {
            t[i++] = arr[p2++];
        }
        for (int j = 0; j < t.length; j++) {
            arr[L + j] = t[j];
        }
    }


    public static void swap(int[] arr, int x, int y) {
        int t = arr[x];
        arr[x] = arr[y];
        arr[y] = t;
    }
}
